<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>部门销售分析</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        //部门
        var departmentTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{name}', '</div></tpl>');
        var departmentStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/department/getAllDepartments?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }])
        });
        departmentStore.load();

        //部门
        var department = new Ext.form.ComboBox({
          store: departmentStore,
          fieldLabel: '业绩部门',
          blankText: "业绩部门必须选择",
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'department_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          //hideTrigger: false,
          readOnly:false,
          tpl: departmentTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          selectOnFocus:true
        });

        var beginDate = new Ext.form.DateField({
          fieldLabel: '日期从',
          name: 'begin_date',
          id: 'dept_begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });

        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'dept_end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var timeSlot = new Ext.form.RadioGroup({
          //width : 220,
          name:'time_slot',
          fieldLabel: '时间分段',
          //hideLabel:true,
          //style:'margin-left:100px;margin-top:20px',
          columns : 4,
          anchor: '100%',
          vertical :false,
          items:[
            {boxLabel:'无',inputValue:'none',name:'time_slot',checked:true},
            {boxLabel:'日',inputValue:'day',name:'time_slot'},
            {boxLabel:'月',inputValue:'month',name:'time_slot'},
            {boxLabel:'年',inputValue:'year',name:'time_slot'}
          ]
        });
        
        var submitButton = new Ext.Button({
          text: '执行统计',
          iconCls: 'finding',
          handler: function(){
            if (queryFormPanel.form.isValid()) {
              statsResultDS.removeAll();
              statsResultDS.baseParams.sell_dept_id = department.getValue();
              statsResultDS.baseParams.begin_date = beginDate.getRawValue();
              statsResultDS.baseParams.end_date = endDate.getRawValue();
              statsResultDS.baseParams.time_slot = timeSlot.getValue().getRawValue();
              statsResultDS.load();
            }
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 65,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1100},
              title: '统计条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      labelWidth: 60,
                      items: [department]
                    },{
                      columnWidth: .14,
                      layout: 'form',
                      border: false,
                      labelWidth: 45,
                      items: [beginDate]
                    }, {
                      columnWidth: .13,
                      layout: 'form',
                      border: false,
                      labelWidth: 30,
                      items: [endDate]
                    }, {
                      columnWidth: .28,
                      layout: 'form',
                      border: false,
                      labelWidth: 60,
                      items: [timeSlot]
                    }, {
                      columnWidth: .12,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .12,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示销售订单操作时的窗口介面。
        //----------------------------------销售订单Grid-------------------------
        //销售订单Grid Store
        var statsResultDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          proxy: new Ext.data.HttpProxy({
            url: '/sale_order/getDeptSalesForReport'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'sell_dept_name'
            },{
              name: 'amount',
              type: 'float'
            },{
              name: 'time_slot',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value;
                }
              }
            }])
        });

        //生成销售订单Grid的CM
        var statsResultSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var statsResultCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),statsResultSM,{
              header: '销售金额',
              dataIndex: 'amount',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 100
            },{
              header: '业绩部门',
              dataIndex: 'sell_dept_name',
              width: 95
            },{
              header: '时间段',
              dataIndex: 'time_slot',
              width: 95
            }
          ]});

        //生成要订单的产品的Grid
        var statsResultGrid = new Ext.grid.GridPanel({
          title:'统计结果列表',
          store: statsResultDS,
          cm: statsResultCM,
          sm: statsResultSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-350,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,      
        });    

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-260,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[queryFormPanel,statsResultGrid]
        });
        workbenchPanel.render("dept_sales_report_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="dept_sales_report_workbench_panel"></div>
  </body>
</html>